/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//package comits.web.client.servlets.demo;
package comits.web.client.servlets.demo;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import comits.business.tiers.dao.EmployeDAO;
import comits.business.tiers.dao.EmployeDAOImpl;
import comits.business.tiers.dao.FrontiereDAO;
import comits.business.tiers.dao.FrontiereDAOImpl;
import comits.business.tiers.metiers.Frontiere;
import comits.business.tiers.requests.manager.ws.RequestsManager;
import comits.business.tiers.utilitaires.LoggingOutputStream;



/**
 * 
 * @author assia
 */
public class ConnectServlet extends HttpServlet {
	
	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		
		
		//obtenir la session         
		HttpSession session = request.getSession();  
		
		String infos =request.getParameter("user");
		String delimiter = "-";
		String str[]=infos.split(delimiter);	
		String tble = str[0];
		String site = str[1];
		String login = str[2];
		int idLogger = Integer.parseInt(str[3]);
		String pass =  request.getParameter("pass");
		          
		//set variable de session      
		session.setAttribute("login", login);  
		session.setAttribute("site", site); 
		session.setAttribute("tble", tble);
		session.setAttribute("pass", pass);

		EmployeDAO eDao = new EmployeDAOImpl();
		FrontiereDAO fDAO = new FrontiereDAOImpl();
		session.setAttribute("idLog", idLogger);
		List<Frontiere> Fa =  fDAO.recupereFrontiere("A");
		List<Frontiere> Fb =  fDAO.recupereFrontiere("B");
		
		session.setAttribute("A",Fa.get(0).getPourcentage());
		int B=Fb.get(0).getPourcentage()-Fa.get(0).getPourcentage();
		session.setAttribute("B",B);
		
//		System.out.println(login);
//		System.out.println(site);
//		System.out.println(pass);
//		System.out.println(tble);
		
		RequestsManager rM = new RequestsManager();
		boolean conected;
		conected = rM.connectionWeb(login, pass, tble);
		session.setAttribute("connexion", conected);
		//session.setAttribute("test", eDao.test());
			if(conected) getServletContext().getRequestDispatcher("/Accueil.jsp").forward(request, response);
			else getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
		
	}

}
